package com.wxiao.exam.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxiao.exam.DataUtils;
import com.wxiao.exam.PageBean;
import com.wxiao.exam.Result;
import com.wxiao.exam.StudentService;
import com.wxiao.exam.mapper.StudentMapper;
import com.wxiao.exam.pojo.StudentDO;
import com.wxiao.exam.pojo.vo.StudentListVO;
import com.wxiao.exam.pojo.vo.StudentVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <br/>
 * Created by wangxiao on 2019/07/05
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    private final static Logger LOG = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Resource
    private StudentMapper studentMapper;

    @Override
    public Result addStudent(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public Result updateStudent(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public Result findDetailStudent(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public Result listStudent(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public Result listStudentPage(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public Result countStudent(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public Result deleteStudent(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public StudentDO getStudentByName(String username) {
        StudentVO studentVO = new StudentVO();
        studentVO.setName(username);
        List<StudentDO> studentDOS = studentMapper.listStudent(studentVO);
        if (!studentDOS.isEmpty()) {
            return studentDOS.get(0);
        } else
            return null;
    }

    @Override
    public PageBean<StudentListVO> listStudentByPage(StudentVO studentVO) {
        //页码越界处理
        Integer count = studentMapper.countStudent(studentVO);
        int max = (count + studentVO.getPageSize() - 1) / (studentVO.getPageSize());
        /*if(studentVO.getPageCode()>max){
            //studentVO.setPageCode(max);
            throw new PageCodeException("页码越界了",max);
        }*/

        PageHelper.startPage(DataUtils.getPageCode(studentVO.getPageCode()+""),studentVO.getPageSize());
        List<StudentListVO> list = studentMapper.listStudentByPage(studentVO);
        PageInfo<StudentListVO> info=new PageInfo(list);
        PageBean<StudentListVO> pageBean= new PageBean<StudentListVO>(list,info.getPageSize(),
                info.getPageNum(),(int)info.getTotal(),info.getSize()
        );
        return pageBean;
    }

}